package com.usernet.product.dao;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.usernet.product.comm.BaseDao;
import com.usernet.product.entity.Software;
import com.usernet.product.utils.Constants;
import com.usernet.product.utils.Page;
import com.usernet.product.utils.PageResult;
import com.usernet.product.utils.ProductConfig;

/**
 * 应用信息
 * 
 * @author wangshuaibo
 * 
 */
public class SoftwareDao extends BaseDao {

	/**
	 * 查询推荐应用总数
	 * 
	 * @return 总数
	 */
	public int getCountForNews() {
		Session session = getSession();
		Transaction tr = null;
		String hql = "select count(*) from Software as soft where soft.status!=? and soft.tops=1";
		try {
			tr = session.beginTransaction();
			Query query = session.createQuery(hql);
			query.setInteger(0, ProductConfig.DIS_STATUS);
			query.setCacheable(true);// 使用缓存
			Object num = query.uniqueResult();
			tr.commit();
			if (num instanceof Number) {
				return ((Number) num).intValue();
			}
		} catch (HibernateException e) {
			if (tr != null) {
				tr.rollback();
			}
		}
		return 0;
	}

	/**
	 * 查询结果总数
	 * 
	 * @param keyword
	 *            关键字
	 * @return
	 */
	public int getCountForSearch(String keyword, int flag) {
		Session session = getSession();
		Transaction tr = null;
		String hql = "select count(*) from Software as soft where soft.status!=? and (soft.name like '%"
				+ keyword
				+ "%' or soft.pinyin like '%"
				+ keyword.toLowerCase()
				+ "%' or soft.tag like '%" + keyword + "%')";
		if (flag != 0) {
			hql += " and soft.type.flag=?";
		}
		try {
			tr = session.beginTransaction();
			Query query = session.createQuery(hql);
			query.setInteger(0, ProductConfig.DIS_STATUS);
			if (flag != 0) {
				query.setInteger(1, flag);
			}
			query.setCacheable(true);// 使用缓存
			Object num = query.uniqueResult();
			tr.commit();
			if (num instanceof Number) {
				return ((Number) num).intValue();
			}
		} catch (HibernateException e) {
			if (tr != null) {
				tr.rollback();
			}
		}
		return 0;
	}

	/**
	 * 排序总数
	 * 
	 * @return 总数
	 */
	public int getCountForSort() {
		Session session = getSession();
		Transaction tr = null;
		String hql = "select count(*) from Software as soft where soft.status!=?";
		try {
			tr = session.beginTransaction();
			Query query = session.createQuery(hql);
			query.setInteger(0, ProductConfig.DIS_STATUS);
			query.setCacheable(true);// 使用缓存
			Object num = query.uniqueResult();
			tr.commit();
			if (num instanceof Number) {
				return ((Number) num).intValue();
			}
		} catch (HibernateException e) {
			if (tr != null) {
				tr.rollback();
			}
		}
		return 0;
	}

	/**
	 * 顶置总数
	 * 
	 * @return 总数
	 */
	public int getCountForTops() {
		Session session = getSession();
		Transaction tr = null;
		String hql = "select count(*) from Software as soft where soft.status!=? and soft.tops=2";
		try {
			tr = session.beginTransaction();
			Query query = session.createQuery(hql);
			query.setInteger(0, ProductConfig.DIS_STATUS);
			query.setCacheable(true);// 使用缓存
			Object num = query.uniqueResult();
			tr.commit();
			if (num instanceof Number) {
				return ((Number) num).intValue();
			}
		} catch (HibernateException e) {
			if (tr != null) {
				tr.rollback();
			}
		}
		return 0;
	}

	/**
	 * 某一类别应用总数
	 * 
	 * @param typeId
	 *            类别ID
	 * @return 总数
	 */
	public int getCountForTypeInfo(int typeId) {
		Session session = getSession();
		Transaction tr = null;
		String hql = "select count(*) from Software as soft where soft.status!=? and soft.type.id=?";
		try {
			tr = session.beginTransaction();
			Query query = session.createQuery(hql);
			query.setInteger(0, ProductConfig.DIS_STATUS);
			query.setInteger(1, typeId);
			query.setCacheable(true);// 使用缓存
			Object num = query.uniqueResult();
			tr.commit();
			if (num instanceof Number) {
				return ((Number) num).intValue();
			}
		} catch (HibernateException e) {
			if (tr != null) {
				tr.rollback();
			}
		}
		return 0;
	}

	/**
	 * 分页获取精品推荐信息
	 * 
	 * @param pageSize
	 *            分页大小
	 * @param pageNo
	 *            页码(从1开始)
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Software> getAllNewByPage(int pageSize, int pageNo) {
		Transaction tr = null;
		Session session = getSession();
		try {
			tr = session.beginTransaction();

			String hql = "select new Software(soft.id,soft.name,soft.size) from Software as soft where soft.status!=? and soft.tops=1 order by soft.recommend asc";// 按推荐顺序
			Query query = session.createQuery(hql);
			query.setInteger(0, ProductConfig.DIS_STATUS);
			query.setFirstResult((pageNo - 1) * pageSize);
			query.setMaxResults(pageSize);
			query.setCacheable(true);// 使用缓存
			List<Software> list = query.list();
			tr.commit();

			return list;
		} catch (Exception ex) {
			if (tr != null) {
				tr.rollback();
			}
			ex.printStackTrace();
			return null;
		}
	}

	/**
	 * 分页获取软件排名信息
	 * 
	 * @param pageSize
	 *            分页大小
	 * @param pageNo
	 *            页码(从1开始)
	 * @param sort
	 *            排序方式 (1:下载量;2:评分;3:发布日期;)
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Software> getAllSortByPage(int pageSize, int pageNo, int sort) {
		Transaction tr = null;
		Session session = getSession();
		try {
			tr = session.beginTransaction();
			String hql = null;
			switch (sort) {
			case 1:
				hql = "select new Software(soft.id,soft.name,soft.size) from Software as soft where soft.status!=? order by soft.count desc,soft.recommend asc";
				break;
			case 2:
				hql = "select new Software(soft.id,soft.name,soft.size) from Software as soft where soft.status!=? order by soft.avg desc,soft.recommend asc";
				break;
			case 3:
				hql = "select new Software(soft.id,soft.name,soft.size) from Software as soft where soft.status!=? order by soft.time desc,soft.recommend asc";
				break;
			}
			Query query = session.createQuery(hql);
			query.setInteger(0, ProductConfig.DIS_STATUS);
			query.setFirstResult((pageNo - 1) * pageSize);
			query.setMaxResults(pageSize);
			query.setCacheable(true);// 使用缓存
			List<Software> list = query.list();
			tr.commit();

			return list;
		} catch (Exception ex) {
			if (tr != null) {
				tr.rollback();
			}
			ex.printStackTrace();
			return null;
		}
	}

	public int getAllSortByPageForWeb(int sort, int flag) {
		Transaction tr = null;
		Session session = getSession();
		try {
			tr = session.beginTransaction();
			String hql = null;
			switch (sort) {
			case 1:
				hql = "select count(*) from Software as soft where soft.status!=? and soft.type.flag=?";
				break;
			case 2:
				hql = "select count(*) from Software as soft where soft.status!=? and soft.type.flag=?";
				break;
			case 3:
				hql = "select count(*) from Software as soft where soft.status!=? and soft.type.flag=?";
				break;
			}
			Query query = session.createQuery(hql);
			query.setInteger(0, ProductConfig.DIS_STATUS);
			query.setInteger(1, flag);
			query.setCacheable(true);// 使用缓存
			Object num = query.uniqueResult();
			tr.commit();
			if (num instanceof Number) {
				return ((Number) num).intValue();
			}
		} catch (Exception ex) {
			if (tr != null) {
				tr.rollback();
			}
			ex.printStackTrace();
		}
		return 0;
	}

	@SuppressWarnings("unchecked")
	public List<Software> getAllSortByPageForWeb(int pageSize, int pageNo,
			int sort, int flag) {
		Transaction tr = null;
		Session session = getSession();
		try {
			tr = session.beginTransaction();
			String hql = null;
			switch (sort) {
			case 1:
				hql = "select soft from Software as soft where soft.status!=? and soft.type.flag=? order by soft.count desc,soft.recommend asc";
				break;
			case 2:
				hql = "select soft from Software as soft where soft.status!=? and soft.type.flag=? order by soft.avg desc,soft.recommend asc";
				break;
			case 3:
				hql = "select soft from Software as soft where soft.status!=? and soft.type.flag=? order by soft.time desc,soft.recommend asc";
				break;
			}
			Query query = session.createQuery(hql);
			query.setInteger(0, ProductConfig.DIS_STATUS);
			query.setInteger(1, flag);
			query.setFirstResult((pageNo - 1) * pageSize);
			query.setMaxResults(pageSize);
			query.setCacheable(true);// 使用缓存
			List<Software> list = query.list();
			tr.commit();

			return list;
		} catch (Exception ex) {
			if (tr != null) {
				tr.rollback();
			}
			ex.printStackTrace();
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public List<Software> getAllCommendByPageForWeb(int pageSize, int pageNo,
			int flag) {
		Transaction tr = null;
		Session session = getSession();
		try {
			tr = session.beginTransaction();
			String hql = "select soft from Software as soft where soft.status!=? and soft.type.flag=? order by soft.time desc"; // 按排序条件降序
			Query query = session.createQuery(hql);
			query.setInteger(0, ProductConfig.DIS_STATUS);
			query.setInteger(1, flag);
			query.setFirstResult((pageNo - 1) * pageSize);
			query.setMaxResults(pageSize);
			query.setCacheable(true);// 使用缓存
			List<Software> list = query.list();
			tr.commit();

			return list;
		} catch (Exception ex) {
			if (tr != null) {
				tr.rollback();
			}
			ex.printStackTrace();
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public List<Software> getAllResByPageForWeb(int pageSize, int pageNo) {
		Transaction tr = null;
		Session session = getSession();
		try {
			tr = session.beginTransaction();
			String hql = "select soft from Software as soft where soft.status!=? order by soft.time desc"; // 按排序条件降序
			Query query = session.createQuery(hql);
			query.setInteger(0, ProductConfig.DIS_STATUS);
			query.setFirstResult((pageNo - 1) * pageSize);
			query.setMaxResults(pageSize);
			query.setCacheable(true);// 使用缓存
			List<Software> list = query.list();
			tr.commit();

			return list;
		} catch (Exception ex) {
			if (tr != null) {
				tr.rollback();
			}
			ex.printStackTrace();
			return null;
		}
	}

	public int getCountCommendByPageForWeb(int flag) {
		Transaction tr = null;
		Session session = getSession();
		try {
			tr = session.beginTransaction();
			String hql = "select count(*) from Software as soft where soft.status!=? and soft.type.flag=?";
			Query query = session.createQuery(hql);
			query.setInteger(0, ProductConfig.DIS_STATUS);
			query.setInteger(1, flag);
			query.setCacheable(true);// 使用缓存
			Object num = query.uniqueResult();
			tr.commit();
			if (num instanceof Number) {
				return ((Number) num).intValue();
			}
		} catch (Exception ex) {
			if (tr != null) {
				tr.rollback();
			}
			ex.printStackTrace();
		}
		return 0;
	}

	/**
	 * 分页获取顶置软件信息
	 * 
	 * @param pageSize
	 *            分页大小
	 * @param pageNo
	 *            页码(从1开始)
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Software> getAllTopByPage(int pageSize, int pageNo, int typeId) {
		Transaction tr = null;
		Session session = getSession();
		try {
			tr = session.beginTransaction();

			String hql = "select new Software(soft.id,soft.name,soft.count,soft.avg,soft.icon) from Software as soft where soft.status!=? and soft.tops=2 and soft.type.id=? order by soft.recommend asc, soft.time desc"; // 按推荐顺序
			Query query = session.createQuery(hql);
			query.setInteger(0, ProductConfig.DIS_STATUS);
			query.setInteger(1, typeId);
			query.setFirstResult((pageNo - 1) * pageSize);
			query.setMaxResults(pageSize);
			query.setCacheable(true);// 使用缓存
			List<Software> list = query.list();
			tr.commit();

			return list;
		} catch (Exception ex) {
			if (tr != null) {
				tr.rollback();
			}
			ex.printStackTrace();
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public List<Software> getAllSearchCommend(int pageSize, int pageNo) {
		Transaction tr = null;
		Session session = getSession();
		try {
			tr = session.beginTransaction();

			String hql = "select new Software(soft.id,soft.name,soft.count,soft.avg,soft.icon) from Software as soft where soft.status!=? and soft.tops=2 order by soft.recommend asc, soft.time desc"; // 按推荐顺序
			Query query = session.createQuery(hql);
			query.setInteger(0, ProductConfig.DIS_STATUS);
			query.setFirstResult((pageNo - 1) * pageSize);
			query.setMaxResults(pageSize);
			query.setCacheable(true);// 使用缓存
			List<Software> list = query.list();
			tr.commit();

			return list;
		} catch (Exception ex) {
			if (tr != null) {
				tr.rollback();
			}
			ex.printStackTrace();
			return null;
		}
	}

	public int getCountTypeInfoByPageAndSort(int flag, int typeId, int sort) {
		Transaction tr = null;
		Session session = getSession();
		try {
			tr = session.beginTransaction();
			String hql = null;
			switch (sort) {
			case 1:
				hql = "select count(*) from Software as soft where soft.status!=? and soft.type.flag=? and type.id=?";
				break;
			case 2:
				hql = "select count(*) from Software as soft where soft.status!=? and soft.type.flag=? and type.id=?";
				break;
			case 3:
				hql = "select count(*) from Software as soft where soft.status!=? and soft.type.flag=? and type.id=?";
				break;
			}
			Query query = session.createQuery(hql);
			query.setInteger(0, ProductConfig.DIS_STATUS);
			query.setInteger(1, flag);
			query.setInteger(2, typeId);
			query.setCacheable(true);// 使用缓存
			Object num = query.uniqueResult();
			tr.commit();
			if (num instanceof Number) {
				return ((Number) num).intValue();
			}
		} catch (Exception ex) {
			if (tr != null) {
				tr.rollback();
			}
			ex.printStackTrace();
		}
		return 0;
	}

	@SuppressWarnings("unchecked")
	public List<Software> getAllTypeInfoByPageAndSort(int pageSize, int pageNo,
			int flag, int typeId, int sort) {
		Transaction tr = null;
		Session session = getSession();
		try {
			tr = session.beginTransaction();
			String hql = null;
			switch (sort) {
			case 1:
				hql = "select soft from Software as soft where soft.status!=? and soft.type.flag=? and type.id=? order by soft.count desc,soft.recommend asc";
				break;
			case 2:
				hql = "select soft from Software as soft where soft.status!=? and soft.type.flag=? and type.id=? order by soft.avg desc,soft.recommend asc";
				break;
			case 3:
				hql = "select soft from Software as soft where soft.status!=? and soft.type.flag=? and type.id=? order by soft.time desc,soft.recommend asc";
				break;
			}
			Query query = session.createQuery(hql);
			query.setInteger(0, ProductConfig.DIS_STATUS);
			query.setInteger(1, flag);
			query.setInteger(2, typeId);
			query.setFirstResult((pageNo - 1) * pageSize);
			query.setMaxResults(pageSize);
			query.setCacheable(true);// 使用缓存
			List<Software> list = query.list();
			tr.commit();

			return list;
		} catch (Exception ex) {
			if (tr != null) {
				tr.rollback();
			}
			ex.printStackTrace();
			return null;
		}
	}

	public int getCountResInfoByPageAndSort(int resId, int sort) {
		Transaction tr = null;
		Session session = getSession();
		try {
			tr = session.beginTransaction();
			String hql = null;
			switch (sort) {
			case 1:
				hql = "select count(*) from Software as soft where soft.status!=? and resources.id=?";
				break;
			case 2:
				hql = "select count(*) from Software as soft where soft.status!=? and resources.id=?";
				break;
			case 3:
				hql = "select count(*) from Software as soft where soft.status!=? and resources.id=?";
				break;
			}
			Query query = session.createQuery(hql);
			query.setInteger(0, ProductConfig.DIS_STATUS);
			query.setInteger(1, resId);
			query.setCacheable(true);// 使用缓存
			Object num = query.uniqueResult();
			tr.commit();
			if (num instanceof Number) {
				return ((Number) num).intValue();
			}

		} catch (Exception ex) {
			if (tr != null) {
				tr.rollback();
			}
			ex.printStackTrace();
		}
		return 0;
	}

	@SuppressWarnings("unchecked")
	public List<Software> getAllResInfoByPageAndSort(int pageSize, int pageNo,
			int resId, int sort) {
		Transaction tr = null;
		Session session = getSession();
		try {
			tr = session.beginTransaction();
			String hql = null;
			switch (sort) {
			case 1:
				hql = "select soft from Software as soft where soft.status!=? and resources.id=? order by soft.count desc,soft.recommend asc";
				break;
			case 2:
				hql = "select soft from Software as soft where soft.status!=? and resources.id=? order by soft.avg desc,soft.recommend asc";
				break;
			case 3:
				hql = "select soft from Software as soft where soft.status!=? and resources.id=? order by soft.time desc,soft.recommend asc";
				break;
			}
			Query query = session.createQuery(hql);
			query.setInteger(0, ProductConfig.DIS_STATUS);
			query.setInteger(1, resId);
			query.setFirstResult((pageNo - 1) * pageSize);
			query.setMaxResults(pageSize);
			query.setCacheable(true);// 使用缓存
			List<Software> list = query.list();
			tr.commit();

			return list;
		} catch (Exception ex) {
			if (tr != null) {
				tr.rollback();
			}
			ex.printStackTrace();
			return null;
		}
	}

	public int getCountResInfoByPage(int sort) {
		Transaction tr = null;
		Session session = getSession();
		try {
			tr = session.beginTransaction();
			String hql = null;
			switch (sort) {
			case 1:
				hql = "select count(*) from Software as soft where soft.status!=?";
				break;
			case 2:
				hql = "select count(*) from Software as soft where soft.status!=?";
				break;
			case 3:
				hql = "select count(*) from Software as soft where soft.status!=?";
				break;
			}
			Query query = session.createQuery(hql);
			query.setInteger(0, ProductConfig.DIS_STATUS);
			query.setCacheable(true);// 使用缓存
			Object num = query.uniqueResult();
			tr.commit();
			if (num instanceof Number) {
				return ((Number) num).intValue();
			}

		} catch (Exception ex) {
			if (tr != null) {
				tr.rollback();
			}
			ex.printStackTrace();
		}
		return 0;
	}

	@SuppressWarnings("unchecked")
	public List<Software> getAllResInfoByPage(int pageSize, int pageNo, int sort) {
		Transaction tr = null;
		Session session = getSession();
		try {
			tr = session.beginTransaction();
			String hql = null;
			switch (sort) {
			case 1:
				hql = "select soft from Software as soft where soft.status!=? order by soft.count desc,soft.recommend asc";
				break;
			case 2:
				hql = "select soft from Software as soft where soft.status!=? order by soft.avg desc,soft.recommend asc";
				break;
			case 3:
				hql = "select soft from Software as soft where soft.status!=? order by soft.time desc,soft.recommend asc";
				break;
			}
			Query query = session.createQuery(hql);
			query.setInteger(0, ProductConfig.DIS_STATUS);
			query.setFirstResult((pageNo - 1) * pageSize);
			query.setMaxResults(pageSize);
			query.setCacheable(true);// 使用缓存
			List<Software> list = query.list();
			tr.commit();

			return list;
		} catch (Exception ex) {
			if (tr != null) {
				tr.rollback();
			}
			ex.printStackTrace();
			return null;
		}
	}

	/**
	 * 搜索
	 */
	@SuppressWarnings("unchecked")
	public List<Software> getAllInfoForSearch(int pageSize, int pageNo,
			String keyword, int flag) {
		Transaction tr = null;
		Session session = getSession();
		try {
			tr = session.beginTransaction();
			String hql = "select soft from Software as soft where soft.status!=? "
					+ "and (soft.name like '%"
					+ keyword
					+ "%' or soft.pinyin like '%"
					+ keyword.toLowerCase()
					+ "%' or soft.tag like '%" + keyword + "%')";
			if (flag != 0) {
				hql += " and soft.type.flag=? ";
			}
			hql += "order by soft.recommend asc, soft.time desc"; // 按时间降序
			Query query = session.createQuery(hql);
			query.setInteger(0, ProductConfig.DIS_STATUS);
			if (flag != 0) {
				query.setInteger(1, flag);
			}
			query.setFirstResult((pageNo - 1) * pageSize);
			query.setMaxResults(pageSize);
			query.setCacheable(true);// 使用缓存
			List<Software> list = query.list();
			tr.commit();

			return list;
		} catch (Exception ex) {
			if (tr != null) {
				tr.rollback();
			}
			ex.printStackTrace();
			return null;
		}
	}

	/**
	 * 分页获取编辑推荐信息(应用OR游戏OR专题)
	 * 
	 * @param pageSize
	 *            分页大小
	 * @param pageNo
	 *            页码(从1开始)
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Software> getAllTopsByPage(int pageSize, int pageNo, int flag) {
		Transaction tr = null;
		Session session = getSession();
		try {
			tr = session.beginTransaction();

			String hql = "select new Software(soft.id,soft.name,soft.count,soft.avg,soft.icon) from Software as soft where soft.status!=? and soft.tops=1 ";
			if (flag != ProductConfig.FLAG_RES) {
				hql += " and soft.type.flag=?";
			}
			hql += " order by soft.recommend asc, soft.time desc";// 按推荐顺序
			Query query = session.createQuery(hql);
			query.setInteger(0, ProductConfig.DIS_STATUS);
			if (flag != ProductConfig.FLAG_RES) {
				query.setInteger(1, flag);
			}
			query.setFirstResult((pageNo - 1) * pageSize);
			query.setMaxResults(pageSize);
			query.setCacheable(true);// 使用缓存
			List<Software> list = query.list();
			tr.commit();

			return list;
		} catch (Exception ex) {
			if (tr != null) {
				tr.rollback();
			}
			ex.printStackTrace();
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public List<Software> getAllTopsByPage(int pageSize, int pageNo) {
		Transaction tr = null;
		Session session = getSession();
		try {
			tr = session.beginTransaction();

			String hql = "select new Software(soft.id,soft.name,soft.count,soft.avg,soft.icon) from Software as soft where soft.status!=? and soft.tops=1 ";
			hql += " order by soft.recommend asc, soft.time desc";// 按推荐顺序
			Query query = session.createQuery(hql);
			query.setInteger(0, ProductConfig.DIS_STATUS);
			query.setFirstResult((pageNo - 1) * pageSize);
			query.setMaxResults(pageSize);
			query.setCacheable(true);// 使用缓存
			List<Software> list = query.list();
			tr.commit();

			return list;
		} catch (Exception ex) {
			if (tr != null) {
				tr.rollback();
			}
			ex.printStackTrace();
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public List<Software> getAllResByPage(int pageSize, int pageNo) {
		Transaction tr = null;
		Session session = getSession();
		try {
			tr = session.beginTransaction();

			String hql = "select new Software(soft.id,soft.name,soft.count,soft.avg,soft.icon) from Software as soft where soft.status!=? and soft.tops=1";
			hql += " order by soft.recommend asc, soft.time desc";// 按推荐顺序
			Query query = session.createQuery(hql);
			query.setInteger(0, ProductConfig.DIS_STATUS);
			query.setFirstResult((pageNo - 1) * pageSize);
			query.setMaxResults(pageSize);
			query.setCacheable(true);// 使用缓存
			List<Software> list = query.list();
			tr.commit();

			return list;
		} catch (Exception ex) {
			if (tr != null) {
				tr.rollback();
			}
			ex.printStackTrace();
			return null;
		}
	}

	public int getCountTopsByPage() {
		Transaction tr = null;
		Session session = getSession();
		try {
			tr = session.beginTransaction();

			String hql = "select count(*) from Software as soft where soft.status!=?";
			Query query = session.createQuery(hql);
			query.setInteger(0, ProductConfig.DIS_STATUS);
			query.setCacheable(true);// 使用缓存
			Object num = query.uniqueResult();
			tr.commit();
			if (num instanceof Number) {
				return ((Number) num).intValue();
			}
		} catch (Exception ex) {
			if (tr != null) {
				tr.rollback();
			}
			ex.printStackTrace();
		}
		return 0;
	}
	@SuppressWarnings("unchecked")
	public PageResult<Software> getAllByPage(Page page) {
		String hql = "select software from Software as software order by time desc,recommend asc";
		return super.getAllByPageAndHql(page, hql);
	}

	/*
	 * 根据条件查询(应用名称,所属分类,是否顶置,是否收费,是否可用)
	 */
	@SuppressWarnings("unchecked")
	public PageResult<Software> getAllByParams(Page page, String softName,
			int typeId, int isTops, int isMoney, int isUsed, int resId) {
		String hql = "select software from Software as software where 1=1";
		if (softName != null && !"".equals(softName.trim())) {
			hql += " and (software.name like '%" + softName
					+ "%' or software.pinyin like '%" + softName.toLowerCase()
					+ "%')";
		}
		if (typeId != -1) {
			hql += " and software.type.id=" + typeId;
		}
		if (resId != -1) {
			hql += " and software.resources.id=" + resId;
		}
		if (isTops != -1) {
			hql += " and software.tops=" + isTops;
		}
		if (isMoney != -1) {
			hql += " and software.share=" + isMoney;
		}
		if (isUsed != -1) {
			hql += " and software.status=" + isUsed;
		}
		hql += " order by software.time desc,software.recommend asc";
		return super.getAllByPageAndHql(page, hql);
	}

	/*
	 * 根据条件查询(应用名称,所属分类,是否顶置,是否收费,是否可用)
	 */
	@SuppressWarnings("unchecked")
	public PageResult<Software> getAllByParams(Page page, String softName,
			int typeId, int isTops, int isMoney, int isUsed) {
		String hql = "select software from Software as software where 1=1";
		if (softName != null && !"".equals(softName.trim())) {
			hql += " and (software.name like '%" + softName
					+ "%' or software.pinyin like '%" + softName.toLowerCase()
					+ "%')";
		}
		if (typeId != -1) {
			hql += " and software.type.id=" + typeId;
		}
		if (isTops != -1) {
			hql += " and software.tops=" + isTops;
		}
		if (isMoney != -1) {
			hql += " and software.share=" + isMoney;
		}
		if (isUsed != -1) {
			hql += " and software.status=" + isUsed;
		}
		hql += " order by id";
		return super.getAllByPageAndHql(page, hql);
	}

	// 精品软件总数
	public int getAllTopsCount() {
		Session session = getSession();
		Transaction tr = null;
		String hql = "select count(*) from Software as soft where soft.status!="
				+ ProductConfig.DIS_STATUS + " and soft.tops=" + Constants.TOPS;
		try {
			tr = session.beginTransaction();
			Query query = session.createQuery(hql);
			query.setCacheable(true);// 使用缓存
			Object num = query.uniqueResult();
			tr.commit();
			if (num instanceof Number) {
				return ((Number) num).intValue();
			}
		} catch (HibernateException e) {
			if (tr != null) {
				tr.rollback();
			}
		}
		return 0;
	}

	// 软件下载次数
	public int getCountById(int softId) {
		Session session = getSession();
		Transaction tr = null;
		String hql = "select soft.count from Software as soft where soft.id=:id";
		try {
			tr = session.beginTransaction();
			Query query = session.createQuery(hql);
			query.setCacheable(true);// 使用缓存
			query.setInteger("id", softId);
			Object num = query.uniqueResult();
			if (num instanceof Number) {
				return ((Number) num).intValue();
			}
		} catch (HibernateException e) {
			if (tr != null) {
				tr.rollback();
			}
		}
		return 0;
	}

	// 查询结果总数
	public int getSearchCount(String keyword) {
		Session session = getSession();
		Transaction tr = null;
		String hql = "select count(*) from Software as soft where soft.status!="
				+ ProductConfig.DIS_STATUS
				+ " and (soft.name like '%"
				+ keyword
				+ "%' or soft.pinyin like '%"
				+ keyword.toLowerCase()
				+ "%' or soft.tag like '%" + keyword + "%') ";
		try {
			tr = session.beginTransaction();
			Query query = session.createQuery(hql);
			// query.setCacheable(true);// 使用缓存
			Object num = query.uniqueResult();
			tr.commit();
			if (num instanceof Number) {
				return ((Number) num).intValue();
			}
		} catch (HibernateException e) {
			if (tr != null) {
				tr.rollback();
			}
		}
		return 0;
	}

	// 排名总数
	public int getSortCount() {
		Session session = getSession();
		Transaction tr = null;
		String hql = "select count(*) from Software as soft where soft.status!="
				+ ProductConfig.DIS_STATUS;
		try {
			tr = session.beginTransaction();
			Query query = session.createQuery(hql);
			query.setCacheable(true);// 使用缓存
			Object num = query.uniqueResult();
			tr.commit();
			if (num instanceof Number) {
				return ((Number) num).intValue();
			}
		} catch (HibernateException e) {
			if (tr != null) {
				tr.rollback();
			}
		}
		return 0;
	}

	// 分类信息-详细软件-总数
	public int getTypeInfoCount(int typeId) {
		Session session = getSession();
		Transaction tr = null;
		String hql = "select count(*) from Software as soft where soft.status!="
				+ ProductConfig.DIS_STATUS + " and soft.type.id=" + typeId;
		try {
			tr = session.beginTransaction();
			Query query = session.createQuery(hql);
			query.setCacheable(true);// 使用缓存
			Object num = query.uniqueResult();
			tr.commit();
			if (num instanceof Number) {
				return ((Number) num).intValue();
			}
		} catch (HibernateException e) {
			if (tr != null) {
				tr.rollback();
			}
		}
		return 0;
	}

	// 专题信息-详细软件-总数
	public int getResourcesInfoCount(int resId) {
		Session session = getSession();
		Transaction tr = null;
		String hql = "select count(*) from Software as soft where soft.status!="
				+ ProductConfig.DIS_STATUS + " and soft.resId=" + resId;
		try {
			tr = session.beginTransaction();
			Query query = session.createQuery(hql);
			query.setCacheable(true);// 使用缓存
			Object num = query.uniqueResult();
			tr.commit();
			if (num instanceof Number) {
				return ((Number) num).intValue();
			}
		} catch (HibernateException e) {
			if (tr != null) {
				tr.rollback();
			}
		}
		return 0;
	}

	// 精品软件
	@SuppressWarnings("unchecked")
	public List<Software> getAllTops(int pageSize, int pageNo) {
		Transaction tr = null;
		Session session = getSession();
		try {
			tr = session.beginTransaction();

			String hql = "select new Software(soft.id,soft.name,soft.icon,soft.develope,soft.avg) from Software as soft where soft.status!="
					+ ProductConfig.DIS_STATUS
					+ " and soft.tops="
					+ Constants.TOPS
					+ " order by soft.recommend asc";// 按推荐顺序
			Query query = session.createQuery(hql);
			query.setFirstResult((pageNo - 1) * pageSize);
			query.setMaxResults(pageSize);
			query.setCacheable(true);// 使用缓存
			List<Software> list = query.list();
			tr.commit();

			return list;
		} catch (Exception ex) {
			tr.rollback();
			ex.printStackTrace();
			return null;
		}
	}

	/**
	 * 软件排名
	 * 
	 * @param pageSize
	 *            分页大小
	 * @param pageNo
	 *            页码(从1开始)
	 * @param sort
	 *            排序方式 (1:下载量;2:评分;3:发布日期;)
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Software> getAllSort(int pageSize, int pageNo, int sort) {
		Transaction tr = null;
		Session session = getSession();
		try {
			tr = session.beginTransaction();
			String hql = null;
			switch (sort) {
			case 1:
				hql = "select new Software(soft.id,soft.name,soft.icon,soft.develope,soft.avg) from Software as soft where soft.status!="
						+ ProductConfig.DIS_STATUS + " order by soft.count desc";
				break;
			case 2:
				hql = "select new Software(soft.id,soft.name,soft.icon,soft.develope,soft.avg) from Software as soft where soft.status!="
						+ ProductConfig.DIS_STATUS + " order by soft.avg desc";
				break;
			case 3:
				hql = "select new Software(soft.id,soft.name,soft.icon,soft.develope,soft.avg) from Software as soft where soft.status!="
						+ ProductConfig.DIS_STATUS + " order by soft.time desc";
				break;
			}

			Query query = session.createQuery(hql);
			query.setFirstResult((pageNo - 1) * pageSize);
			query.setMaxResults(pageSize);
			query.setCacheable(true);// 使用缓存
			List<Software> list = query.list();
			tr.commit();

			return list;
		} catch (Exception ex) {
			tr.rollback();
			ex.printStackTrace();
			return null;
		}
	}

	// 分类信息-详细软件列表
	@SuppressWarnings("unchecked")
	public List<Software> getAllTypeInfo(int pageSize, int pageNo, int typeId) {
		Transaction tr = null;
		Session session = getSession();
		try {
			tr = session.beginTransaction();
			String hql = "select new Software(soft.id,soft.name,soft.icon,soft.develope,soft.avg) from Software as soft where soft.status!="
					+ ProductConfig.DIS_STATUS
					+ " and soft.type.id="
					+ typeId
					+ " order by soft.time desc";
			Query query = session.createQuery(hql);
			query.setFirstResult((pageNo - 1) * pageSize);
			query.setMaxResults(pageSize);
			query.setCacheable(true);// 使用缓存
			List<Software> list = query.list();
			tr.commit();

			return list;
		} catch (Exception ex) {
			tr.rollback();
			ex.printStackTrace();
			return null;
		}
	}

	// 专题信息-详细软件列表
	@SuppressWarnings("unchecked")
	public List<Software> getAllResourcesInfo(int pageSize, int pageNo,
			int resId) {
		Transaction tr = null;
		Session session = getSession();
		try {
			tr = session.beginTransaction();
			String hql = "select new Software(soft.id,soft.name,soft.icon,soft.develope,soft.avg) from Software as soft where soft.status!="
					+ ProductConfig.DIS_STATUS
					+ " and soft.resources.id="
					+ resId + " order by soft.time desc";
			Query query = session.createQuery(hql);
			query.setFirstResult((pageNo - 1) * pageSize);
			query.setMaxResults(pageSize);
			query.setCacheable(true);// 使用缓存
			List<Software> list = query.list();
			tr.commit();

			return list;
		} catch (Exception ex) {
			tr.rollback();
			ex.printStackTrace();
			return null;
		}
	}

	// 搜索
	@SuppressWarnings("unchecked")
	public List<Software> getAllSearch(int pageSize, int pageNo, String keyword) {
		Transaction tr = null;
		Session session = getSession();
		try {
			tr = session.beginTransaction();
			String hql = "select new Software(soft.id,soft.name,soft.icon,soft.develope,soft.avg) from Software as soft where soft.status!="
					+ ProductConfig.DIS_STATUS
					+ " and (soft.name like '%"
					+ keyword
					+ "%' or soft.pinyin like '%"
					+ keyword.toLowerCase()
					+ "%' or soft.tag like '%"
					+ keyword
					+ "%')";
			Query query = session.createQuery(hql);
			query.setFirstResult((pageNo - 1) * pageSize);
			query.setMaxResults(pageSize);
			// query.setCacheable(true);// 使用缓存
			List<Software> list = query.list();
			tr.commit();

			return list;
		} catch (Exception ex) {
			tr.rollback();
			ex.printStackTrace();
			return null;
		}
	}

	// 编辑推荐
	@SuppressWarnings("unchecked")
	public List<Software> getAllRecommend(int pageSize, int pageNo) {
		Transaction tr = null;
		Session session = getSession();
		try {
			tr = session.beginTransaction();

			String hql = "select new Software(soft.id,soft.name,soft.icon,soft.develope,soft.avg) from Software as soft where soft.status!="
					+ ProductConfig.DIS_STATUS
					+ " and soft.tops="
					+ Constants.RECOMEND
					+ " order by soft.recommend asc";// 按推荐顺序
			Query query = session.createQuery(hql);
			query.setFirstResult((pageNo - 1) * pageSize);
			query.setMaxResults(pageSize);
			query.setCacheable(true);// 使用缓存
			List<Software> list = query.list();
			tr.commit();

			return list;
		} catch (Exception ex) {
			tr.rollback();
			ex.printStackTrace();
			return null;
		}
	}

	// 编辑推荐总数
	public int getAllRecommendCount() {
		Session session = getSession();
		Transaction tr = null;
		String hql = "select count(*) from Software as soft where soft.status!="
				+ ProductConfig.DIS_STATUS
				+ " and soft.tops="
				+ Constants.RECOMEND;
		try {
			tr = session.beginTransaction();
			Query query = session.createQuery(hql);
			query.setCacheable(true);// 使用缓存
			Object num = query.uniqueResult();
			tr.commit();
			if (num instanceof Number) {
				return ((Number) num).intValue();
			}
		} catch (HibernateException e) {
			if (tr != null) {
				tr.rollback();
			}
		}
		return 0;
	}

	// 最新上架
	@SuppressWarnings("unchecked")
	public List<Software> getAllHot(int pageSize, int pageNo) {
		Transaction tr = null;
		Session session = getSession();
		try {
			tr = session.beginTransaction();

			String hql = "select new Software(soft.id,soft.name,soft.icon,soft.develope,soft.avg) from Software as soft where soft.status!="
					+ ProductConfig.DIS_STATUS + " order by soft.time desc";// 按推荐顺序
			Query query = session.createQuery(hql);
			query.setFirstResult((pageNo - 1) * pageSize);
			query.setMaxResults(pageSize);
			query.setCacheable(true);// 使用缓存
			List<Software> list = query.list();
			tr.commit();

			return list;
		} catch (Exception ex) {
			tr.rollback();
			ex.printStackTrace();
			return null;
		}
	}

	// 最新上架总数
	public int getAllHotCount() {
		Session session = getSession();
		Transaction tr = null;
		String hql = "select count(*) from Software as soft where soft.status!="
				+ ProductConfig.DIS_STATUS;
		try {
			tr = session.beginTransaction();
			Query query = session.createQuery(hql);
			query.setCacheable(true);// 使用缓存
			Object num = query.uniqueResult();
			tr.commit();
			if (num instanceof Number) {
				return ((Number) num).intValue();
			}
		} catch (HibernateException e) {
			if (tr != null) {
				tr.rollback();
			}
		}
		return 0;
	}
}
